import { connect } from 'umi';
import styles from './index.less';
import Header from './components/Header';
import Main from './components/Main';
import { TRDefault } from '@/components/index.js';
import { Spin } from 'antd';
import { useEffect } from 'react';

const RollingBatch = ({ loadingNum, showMain, hasModified, dispatch }) => {
  // 刷新页面后，必须点击下当前页面再点击刷新按钮才会触发监听时间
  window.addEventListener('beforeunload', (e) => {
    if (hasModified) {
      let confirmationMessage = '当前工作将不被保存，继续执行此操作？';
      (e || window.event).returnValue = confirmationMessage;
      return confirmationMessage;
    }
  });

  useEffect(() => {
    dispatch({
      type: 'rollingBatch/getStationLevelThreeEffect',
    });
    dispatch({
      type: 'rollingBatch/fetchTradeTimeEffect',
    });
  }, []);

  return (
    <Spin spinning={loadingNum !== 0}>
      <div className={styles.container}>
        <Header />
        {showMain ? <Main /> : <TRDefault type="empty" />}
      </div>
    </Spin>
  );
};

export default connect(({ rollingBatch }) => ({
  currentProgramId: rollingBatch.currentProgramId,
  loadingNum: rollingBatch.loadingNum,
  showMain: rollingBatch.showMain,
  hasModified: rollingBatch.hasModified,
}))(RollingBatch);
